*** The file builds spending shares by across all relevant product modules
*** by price decile cells of the product space 
*** the difference with 4b is that we now build leave-one-out spending share
*** for each product module by price decile, where the dimension of household
*** group is age-education-race-children (and no longer state, where we use 
*** the leave-one-out)

cd "D:\Dropbox\unequal_gains\main_data\HMS"
global db "D:\Dropbox\unequal_gains\main_data"
global Section4 "D:\Dropbox\unequal_gains\QJE revision plan\analysis\section4_data"

* We get total spending patterns from 4b, but we collapse differently
use "$Section4/age_educ_race_children_state_shares", clear
* this dataset gives total spending by product module quality-rank in each state by 
* age-education-race-children; difference with 4b is that now we treat "states" 
* in the location space, not in the household group space 

* initial dataset 
clear
set obs 1
gen fips_state_code=.
save "$Section4/age_educ_race_children_loostate_shares", replace

* now we want to build leave-one-out spending shares by age-education-children-race in each state
foreach i of numlist 1(1)56 {

use "$Section4/age_educ_race_children_state_shares", clear
* drop state to compute leave-one-out share
drop if fips_state_code==`i'

collapse (sum) total_spending, by(product_module_code quality_rank age_bin education race children) fast
gen fips_state_code=`i'
rename total_spending loo_total_spending
append using "$Section4/age_educ_race_children_loostate_shares"
save "$Section4/age_educ_race_children_loostate_shares", replace
}

* now clean file by keeping only proper states
use "$Section4/age_educ_race_children_state_shares", clear
keep fips_state_code
duplicates drop 
merge 1:m fips_state_code using "$Section4/age_educ_race_children_loostate_shares"
keep if _merge==3
drop _merge
save "$Section4/age_educ_race_children_loostate_shares", replace

